package com.imut.lagain.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imut.lagain.entity.MoodThief;
import com.imut.lagain.service.IMoodThiefService;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 心情小偷控制器
 */

@RestController
@RequestMapping("/api/mood-thieves")
@RequiredArgsConstructor
public class MoodThiefController {
    private static final Logger log = LoggerFactory.getLogger(MoodThiefController.class);

    
    private final IMoodThiefService moodThiefService;
    
    /**
     * 创建心情小偷
     * @param moodThief 心情小偷信息
     * @return 创建结果
     */
    @PostMapping
    public ResponseEntity<Map<String, Object>> createMoodThief(@RequestBody MoodThief moodThief) {
        Map<String, Object> response = new HashMap<>();
        
        try {
            moodThief.setCreateTime(LocalDateTime.now());
            moodThief.setUpdateTime(LocalDateTime.now());
            boolean success = moodThiefService.save(moodThief);
            
            if (success) {
                response.put("success", true);
                response.put("message", "心情小偷创建成功");
                response.put("data", moodThief);
            } else {
                response.put("success", false);
                response.put("message", "心情小偷创建失败");
            }
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("创建心情小偷失败", e);
            response.put("success", false);
            response.put("message", "创建心情小偷失败: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }
    
    /**
     * 更新心情小偷
     * @param id 心情小偷ID
     * @param moodThief 心情小偷信息
     * @return 更新结果
     */
    @PutMapping("/{id}")
    public ResponseEntity<Map<String, Object>> updateMoodThief(@PathVariable Long id, @RequestBody MoodThief moodThief) {
        Map<String, Object> response = new HashMap<>();
        
        try {
            MoodThief existing = moodThiefService.getById(id);
            if (existing == null) {
                response.put("success", false);
                response.put("message", "心情小偷不存在");
                return ResponseEntity.badRequest().body(response);
            }
            
            moodThief.setId(id);
            moodThief.setUpdateTime(LocalDateTime.now());
            boolean success = moodThiefService.updateById(moodThief);
            
            if (success) {
                response.put("success", true);
                response.put("message", "心情小偷更新成功");
                response.put("data", moodThief);
            } else {
                response.put("success", false);
                response.put("message", "心情小偷更新失败");
            }
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("更新心情小偷失败", e);
            response.put("success", false);
            response.put("message", "更新心情小偷失败: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }
    
    /**
     * 删除心情小偷
     * @param id 心情小偷ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> deleteMoodThief(@PathVariable Long id) {
        Map<String, Object> response = new HashMap<>();
        
        try {
            MoodThief existing = moodThiefService.getById(id);
            if (existing == null) {
                response.put("success", false);
                response.put("message", "心情小偷不存在");
                return ResponseEntity.badRequest().body(response);
            }
            
            boolean success = moodThiefService.removeById(id);
            
            if (success) {
                response.put("success", true);
                response.put("message", "心情小偷删除成功");
            } else {
                response.put("success", false);
                response.put("message", "心情小偷删除失败");
            }
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("删除心情小偷失败", e);
            response.put("success", false);
            response.put("message", "删除心情小偷失败: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }
    
    /**
     * 获取心情小偷详情
     * @param id 心情小偷ID
     * @return 心情小偷信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getMoodThief(@PathVariable Long id) {
        Map<String, Object> response = new HashMap<>();
        
        try {
            MoodThief moodThief = moodThiefService.getById(id);
            if (moodThief == null) {
                response.put("success", false);
                response.put("message", "心情小偷不存在");
                return ResponseEntity.badRequest().body(response);
            }
            
            response.put("success", true);
            response.put("message", "获取心情小偷成功");
            response.put("data", moodThief);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("获取心情小偷失败", e);
            response.put("success", false);
            response.put("message", "获取心情小偷失败: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }
    
    /**
     * 分页查询心情小偷列表
     * @param page 页码
     * @param size 每页大小
     * @param thiefType 小偷类型（可选）
     * @param displayName 显示名称关键字（可选）
     * @return 心情小偷列表
     */
    @GetMapping
    public ResponseEntity<Map<String, Object>> getMoodThiefList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String thiefType,
            @RequestParam(required = false) String displayName) {
        Map<String, Object> response = new HashMap<>();
        
        try {
            Page<MoodThief> pageRequest = new Page<>(page, size);
            IPage<MoodThief> moodThiefPage;
            
            if (thiefType != null) {
                MoodThief thief = moodThiefService.findByThiefType(thiefType);
                if (thief != null) {
                    Page<MoodThief> resultPage = new Page<>(page, size);
                    resultPage.setRecords(java.util.Arrays.asList(thief));
                    resultPage.setTotal(1);
                    moodThiefPage = resultPage;
                } else {
                    Page<MoodThief> resultPage = new Page<>(page, size);
                    resultPage.setRecords(new java.util.ArrayList<>());
                    resultPage.setTotal(0);
                    moodThiefPage = resultPage;
                }
            } else if (displayName != null) {
                moodThiefPage = moodThiefService.findByDisplayNameLike(displayName, pageRequest);
            } else {
                moodThiefPage = moodThiefService.findActiveMoodThieves(pageRequest);
            }
            
            response.put("success", true);
            response.put("message", "获取心情小偷列表成功");
            response.put("data", moodThiefPage);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("获取心情小偷列表失败", e);
            response.put("success", false);
            response.put("message", "获取心情小偷列表失败: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }
}

